<!-- 父级页面:控制除登录页以外所有页面共有的左侧菜单栏与顶部条 -->
<template>
  <div class="home-container">
    <!-- 左侧菜单栏 -->
    <div class="sidebar" :class="{ 'sidebar-collapsed': isCollapse }">
      <el-menu
          class="custom-menu"
          :class="{ 'menu-collapsed': isCollapse }"
          background-color="#2F4F4F"
          text-color="#e6e6e6"
          active-text-color="#fff"
          :collapse="isCollapse"
          unique-opened
          router
          :collapse-transition="false"
      >
        <!-- 菜单的顶部条 -->
        <div class="logo-area">
          <div class="logo-content">
            <el-avatar src="/imgs/login/img.png" class="logo-img"></el-avatar>
            <transition>
              <span v-if="!isCollapse" class="logo-text">社区管理系统</span>
            </transition>
          </div>
        </div>

        <!-- 菜单项 -->
        <el-menu-item index="/index">
          <el-icon class="menu-icon"><HomeFilled /></el-icon>
          <span v-if="!isCollapse" style="font-size: 16px;font-style: oblique;padding-left: 2px">首页</span>
        </el-menu-item>

        <el-sub-menu index="2">
          <template #title>
            <el-icon class="menu-icon"><User /></el-icon>
            <span style="font-size: 16px;font-style: oblique;padding-left: 2px">用户管理</span>
          </template>
          <el-menu-item index="/user" class="submenu-item">用户列表</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="3">
          <template #title>
            <el-icon class="menu-icon"><ShoppingCart /></el-icon>
            <span style="font-size: 16px;font-style: oblique;padding-left: 2px">商品管理</span>
          </template>
          <el-menu-item index="/product" class="submenu-item">商品列表</el-menu-item>
          <el-menu-item index="/product/add" class="submenu-item">添加商品</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="4">
          <template #title>
            <el-icon class="menu-icon"><List /></el-icon>
            <span style="font-size: 16px;font-style: oblique;padding-left: 2px">订单管理</span>
          </template>
          <el-menu-item index="/order" class="submenu-item">订单列表</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="5">
          <template #title>
            <el-icon class="menu-icon"><Message /></el-icon>
            <span style="font-size: 16px;font-style: oblique;padding-left: 2px">意见反馈</span>
          </template>
          <el-menu-item index="/feedback" class="submenu-item">意见列表</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="6">
          <template #title>
            <el-icon class="menu-icon"><Files /></el-icon>
            <span style="font-size: 16px;font-style: oblique;padding-left: 2px">举报管理</span>
          </template>
          <el-menu-item index="/report" class="submenu-item">举报列表</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="7">
          <template #title>
            <el-icon class="menu-icon"><Failed /></el-icon>
            <span style="font-size: 16px;font-style: oblique;padding-left: 2px">异常管理</span>
          </template>
          <el-menu-item index="/abnormal" class="submenu-item">异常列表</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="8">
          <template #title>
            <el-icon class="menu-icon"><Tickets /></el-icon>
            <span style="font-size: 16px;font-style: oblique;padding-left: 2px">公告管理</span>
          </template>
          <el-menu-item index="/announcement" class="submenu-item">公告列表</el-menu-item>
          <el-menu-item index="/announcementAdd" class="submenu-item">添加公告</el-menu-item>
        </el-sub-menu>
      </el-menu>
    </div>

    <!-- 右侧顶部条与主体内容(可变区域) -->
    <div class="main-content" :class="{ 'main-content-expanded': isCollapse }">
      <!-- 右侧顶部条 -->
      <el-header class="header">
        <div class="header-content">
          <!-- 折叠图标按钮 -->
          <div class="collapse-btn" @click="changeCollapse">
            <el-icon><Expand /></el-icon>
          </div>

          <div class="header-spacer"></div>

          <!-- 登录人姓名与退出登录 -->
          <div class="user-info">
            <el-dropdown trigger="click" class="user-dropdown">
              <span class="user-name">
                <el-avatar :size="30" class="user-avatar"><User/></el-avatar>
                <span class="name-text">{{ auth }}</span>
                <el-icon><arrow-down /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="logout" class="logout-item">
                    <el-icon><SwitchButton /></el-icon>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>

        <!-- 面包屑导航 -->
        <div class="breadcrumb-container">
          <el-breadcrumb separator="/" class="breadcrumb">
            <el-breadcrumb-item>
              <router-link to="/index" style="font-weight: bold; color: inherit;">
                首页
              </router-link>
            </el-breadcrumb-item>
            <el-breadcrumb-item v-for="(item, index) in breadcrumb" :key="index">{{ item }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
      </el-header>

      <!-- 右侧主体内容(可变区域) -->
      <el-main class="content-area">
        <router-view />
      </el-main>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import {ElMessage, ElMessageBox} from "element-plus";

// 定义变量用来控制左侧菜单栏是否折叠
const isCollapse = ref(false);
const changeCollapse = () => {
  isCollapse.value = !isCollapse.value;
}

// 获取当前登录的用户数据
const auth = ref(getAuth());

// 定义退出登录的方法
const logout = () => {
  ElMessageBox.confirm('您确认要退出系统吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    localStorage.removeItem('auth');
    window.location.href = '/login';
    auth.value = '';
    ElMessage({
      type: 'success',
      message: '退出成功',
    });
  }).catch(() => {
    // 取消退出
  });
}

// 定义对象用来维护路由path与面包屑的对应关系
let map = {
  '/user': ['用户管理', '用户列表'],
  '/product': ['商品管理', '商品列表'],
  '/product/add': ['商品管理', '添加商品'],
  '/order': ['订单管理', '订单列表'],
  '/feedback': ['意见反馈', '意见列表'],
  '/feedback/process': ['意见反馈', '意见列表', '处理详情'],
  '/report': ['举报管理', '举报列表'],
  '/reportUpdate': ['举报管理', '举报列表','举报详情'],
  '/abnormal': ['异常管理', '异常列表'],
  '/abnormal/process': ['异常管理', '异常列表', '处理详情'],
  '/announcement': ['公告管理', '公告列表'],
  '/announcementAdd': ['公告管理', '添加公告'],
};

// 定义对象用来保存面包屑当前展示的值
const breadcrumb = ref([]);
const route = useRoute();
onMounted(() => {
  updateBreadcrumb(route);
})

// 定义更新面包屑导航值的方法
const updateBreadcrumb = (route) => {
  const path = route.path;
  breadcrumb.value = map[path] || [];
}

// 获取路由守卫,监听路由,只要path发生了改变(包括点击左侧菜单栏),就立即更新面包屑的值,再跳转
const router = useRouter();
router.beforeEach((to, from, next) => {
  updateBreadcrumb(to);
  next();
})
</script>

<style scoped>
.home-container {
  display: flex;
  min-height: 100vh;
  background-color: #f5f7fa;
}

/* 侧边栏样式 */
.sidebar {
  width: 220px;
  background-color: #2F4F4F;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.15);
}

.sidebar-collapsed {
  width: 64px;
}

.custom-menu {
  width: 100%;
  height: 100vh;
  border-right: none;
}

.custom-menu:not(.el-menu--collapse) {
  width: 220px;
}

.menu-collapsed {
  width: 64px;
}

/* 菜单顶部logo区域 */
.logo-area {
  padding: 15px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.logo-content {
  display: flex;
  align-items: center;
  margin-top:30px;
  margin-bottom:30px;
  margin-left:25px;
}

.logo-img {
  width: 35px;
  height: 35px;
  background-color: #409EFF;
}

.logo-text {
  color: #fff;
  font-weight: bold;
  margin-left: 10px;
  font-size: 20px;
}

/* 菜单图标样式 */
.menu-icon {
  font-size: 18px;
  margin-left: 3px;
  margin-right: 8px;
}

/* 子菜单项样式 */
.submenu-item {
  background-color: rgb(47,69,69) !important;
  font-style: italic;
}

.submenu-item:hover {
  background-color: #1e2a38 !important;
}

/* 主内容区域样式 */
.main-content {
  flex: 1;
  width: calc(100% - 220px);
  background-color: #FFFAF0;
}

.main-content-expanded {
  width: calc(100% - 64px);
}

/* 顶部header样式 */
.header {
  height: auto;
  background-color: #fff;
  padding: 0;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.header-content {
  display: flex;
  align-items: center;
  padding: 20px 20px;
  border-bottom: 1px solid #eee;
  background-color: #F5F5F5;
}

/* 折叠按钮样式 */
.collapse-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 18px;
}

.collapse-btn:hover {
  background-color: #f5f7fa;
  color: #409EFF;
}

.header-spacer {
  flex: 1;
}

/* 用户信息样式 */
.user-info {
  display: flex;
  align-items: center;
}

.user-dropdown {
  cursor: pointer;
}

.user-name {
  display: flex;
  align-items: center;
  font-size: 14px;
  font-weight: 500;
}

.user-avatar {
  margin-right: 8px;
  background-color: gray;
}

.name-text {
  margin-right: 5px;
}

.logout-item {
  display: flex;
  align-items: center;
}

/* 面包屑样式 */
.breadcrumb-container {
  padding: 15px 20px;
  background-color: #f5f7fa;
  border-bottom: 1px solid #eee;
  font-size: 16px;
}

.breadcrumb {
  font-size: 16px;
}

/* 内容区域样式 */
.content-area {
  height: calc(100vh - 120px);
  padding: 20px;
  overflow-y: auto;
  background-color: #fff;
  margin: 15px;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}
</style>
